package com.yanqu.road.entity.activity.newyear2023.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newyear2023.cross.RobRedPacketUser;
import com.yanqu.road.entity.activity.twins.config.*;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FireWorkConfig {

    private RandomHelper randomHelper = new RandomHelper();

    private int maxTableCount = 500;

    private ActivityInfo activityInfo;

    //放烟花

    private int scoreId;

    private int fireWorkItemId;

    private int fireWorkSpecialItemId;

    private int specialChance;

    private int tenFireIntegral;

    private List<Integer> fireColoerList;

    private List<Integer> spItemIdList;

    //年夜饭
    //红包界面客户端假表现的玩家的数量
    private int robotUserCount;
    //紅包是不是開啓
    private boolean isRedPacketOpen;
    //开启分数条件大于等于
    private List<Long> openTableScoreList;
    //红包参数（每个玩家同时开年夜饭数量上限|抢红包上限数量|发红包文本字符|抢红包文本字符|单个红包元宝数|单个红包人数|单个红包保底元宝数|最小随机值千分比|最大随机值千分比）
    private List<Integer> redPacketParamList = new ArrayList<>();



    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public void initConfig(Map<String, ActivityConfig> activityConfigMap) {
        if(activityInfo.getChildType() == 1){

            scoreId = activityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue();
            fireWorkItemId = activityConfigMap.get(eActivityConfigType.FIREWORKS_FIRE_NOMAL_ITEM_ID.getName()).getIntValue();
            fireWorkSpecialItemId = activityConfigMap.get(eActivityConfigType.FIREWORKS_FIRE_SPECIAL_ITEM_ID.getName()).getIntValue();
            specialChance = activityConfigMap.get(eActivityConfigType.FIREWORKS_SPECIAL_PROBABILITY.getName()).getIntValue();
            tenFireIntegral = activityConfigMap.get(eActivityConfigType.FIREWORKS_FIRE_TEN_LIMIT.getName()).getIntValue();

            fireColoerList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.FIREWORKS_FIRE_NOMAL_COLOR.getName()).getValue(),"\\|");
            spItemIdList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.FIREWORKS_FIRE_SP_ITEM_ID.getName()).getValue(),"\\|");

        }else if(activityInfo.getChildType() == 2){

            openTableScoreList = StringUtils.stringToLongList(activityConfigMap.get(eActivityConfigType.FIREWORKS_DINNER_OPEN_LIMIT.getName()).getValue(),"\\|");
            //15|50|24|24|5000|50|10|100|1900
            redPacketParamList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.FIREWORKS_DINNER_RED_BAG_PARAM.getName()).getValue(),"\\|");

            robotUserCount = activityConfigMap.get(eActivityConfigType.FIREWORKS_DINNER_ROBOT_USER_COUNT.getName()).getIntValue();

            isRedPacketOpen = activityConfigMap.get(eActivityConfigType.FIREWORKS_DINNER_RED_PACKET_OPEN.getName()).getIntValue() == 1;

        }
    }

    public ActivityInfo getActivityInfo(){
        return activityInfo;
    }

    public int getActivityId(){
        return activityInfo.getActivityId();
    }

    public int getScoreId(){
        return scoreId;
    }

    public int getSpFireId(){
        return fireWorkSpecialItemId;
    }

    public int getNmFireId(){
        return fireWorkItemId;
    }

    public boolean isRedPacketOpen(){
        return isRedPacketOpen;
    }

    public int getMaxTableCount(){
        return maxTableCount;
    }

    public int getRobotUserCount(){
        return robotUserCount;
    }

    public int getLimitOpenSameTime(){
        return redPacketParamList.get(0);
    }

    public int getLimitRobTimes(){
        return redPacketParamList.get(1);
    }

    public int getLimitOpenNoteLength(){
        return redPacketParamList.get(2);
    }

    public int getLimitRobNoteLength(){
        return redPacketParamList.get(3);
    }

    public int getRedPacketAllValue(){
        return redPacketParamList.get(4);
    }

    public int getRedPacketUserCount(){
        return redPacketParamList.get(5);
    }

    public int getRedPacketRobMinValue(){
        return redPacketParamList.get(6);
    }


    public int getRedPacketRobMinParam(){
        return redPacketParamList.get(7);
    }

    public int getRedPacketRobMaxParam(){
        return redPacketParamList.get(8);
    }


    public boolean isSpecialChance(){
        int randomValue = randomHelper.next(0,1000);
        return randomValue < specialChance;
    }

    //当前积分，要开第几场
    public long getOpenLimitScore(int count){
        if(count - 1 >= 0 && count - 1 < openTableScoreList.size()){
            return openTableScoreList.get(count - 1);
        }else{
            return Long.MAX_VALUE;
        }
    }

    //随机一个玩家没抢过的桌子
    public int randomOneTable(int min,int max){
        return randomHelper.next(min,max);
    }

    //改了
    //剩余随机总值=参数1-参数2*参数3-∑随机值
    //随机值=RANDBETWEEN（剩余随机总值/剩余随机人数*参数4，剩余随机总值/剩余随机人数*参数5）
    //红包金额=保底值+随机值
    //最后一个红包金额=保底值+剩余随机总值
    public int randomRedPacketValueNew(int remainValue, Map<Long, RobRedPacketUser> robUserMap){
        //配错错误了，保底都不够发，直接发保底
        if(remainValue < getRedPacketRobMinValue()){
            return getRedPacketRobMinValue();
        }
        //最后一个人直接全给
        if(getRedPacketUserCount() - robUserMap.size() == 1){
            return remainValue;
        }
        int randomRemainValue = getRedPacketAllValue() - getRedPacketRobMinValue() * getRedPacketUserCount();
        for(RobRedPacketUser robUser : robUserMap.values()){
            randomRemainValue = randomRemainValue - (robUser.getValue() - getRedPacketRobMinValue());
        }
        int remainUserCount = getRedPacketUserCount() - robUserMap.size();
        int randomMinValue = (randomRemainValue * getRedPacketRobMinParam())/(remainUserCount * 1000);
        int randomMaxValue = (randomRemainValue * getRedPacketRobMaxParam())/(remainUserCount * 1000);
        return randomHelper.next(randomMinValue,randomMaxValue + 1) + getRedPacketRobMinValue();
    }

}
